package _022_2_12

func insert(intervals [][]int, newInterval []int) [][]int {
	result := make([][]int, 0)

	l := 0

	for l < len(intervals) && intervals[l][1] < newInterval[0] {
		result = append(result, intervals[l])
		l++
	}

	for l < len(intervals) && intervals[l][0] <= newInterval[1] {
		if intervals[l][0] < newInterval[0] {
			newInterval[0] = intervals[l][0]
		}

		if intervals[l][1] > newInterval[1] {
			newInterval[1] = intervals[l][1]
		}
		l++
	}

	result = append(result, newInterval)

	for l < len(intervals) {
		result = append(result, intervals[l])
		l++
	}
	return result
}
